/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openFuyao is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package registry

import (
	"testing"
)

// 测试 transport 前缀检测
func TestHasTransportPrefix(t *testing.T) {
	tests := []struct {
		ref      string
		expected bool
	}{
		{"docker://registry.example.com/image:tag", true},
		{"oci:/path/to/layout:ref", true},
		{"dir:/path/to/directory", true},
		{"oci-archive:/path/to/archive.tar", true},
		{"registry.example.com/image:tag", false},
		{"localhost:5000/image:tag", false},
		{"", false},
	}

	for _, tt := range tests {
		if got := hasTransportPrefix(tt.ref); got != tt.expected {
			t.Errorf("hasTransportPrefix(%q) = %v, want %v", tt.ref, got, tt.expected)
		}
	}
}

// 测试 SystemContext 创建
func TestNewSystemContext(t *testing.T) {
	ctx, err := newSystemContext()
	if err != nil {
		t.Fatalf("newSystemContext returned error: %v", err)
	}
	if ctx == nil {
		t.Fatal("newSystemContext returned nil")
	}
	if ctx.DockerRegistryUserAgent != "bke/v1.0.0" {
		t.Errorf("DockerRegistryUserAgent = %q, want %q", ctx.DockerRegistryUserAgent, "bke/v1.0.0")
	}
}

// 测试无效镜像的多架构检测
func TestIsMultiArchManifestsInvalidImage(t *testing.T) {
	if IsMultiArchManifests(false, "invalid-registry.example.com/nonexistent:tag") {
		t.Error("IsMultiArchManifests should return false for invalid image")
	}
}

// 测试 Options 结构体基本功能
func TestOptions(t *testing.T) {
	t.Run("valid options", func(t *testing.T) {
		op := Options{
			Source:        "docker://source:tag",
			Target:        "docker://target:tag",
			Arch:          "amd64",
			MultiArch:     false,
			SrcTLSVerify:  true,
			DestTLSVerify: true,
		}

		if op.Source == "" {
			t.Error("Source should not be empty")
		}
		if op.Target == "" {
			t.Error("Target should not be empty")
		}
	})

	t.Run("multi-arch options", func(t *testing.T) {
		op := Options{
			Source:        "docker://source:tag",
			Target:        "docker://target:tag",
			Arch:          "",
			MultiArch:     true,
			SrcTLSVerify:  true,
			DestTLSVerify: true,
		}

		if !op.MultiArch {
			t.Error("MultiArch should be true")
		}
		if op.Arch != "" {
			t.Error("Arch should be empty for multi-arch")
		}
	})
}
